package com.liu.qqRobot.q.handler;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.common.utils.HtmlToImageUtils;
import com.liu.common.utils.QQClientUtils;
import com.liu.mybatisPlus.q.entity.base.QGroupUserOperationQuantity;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import freemarker.template.TemplateException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
public class MyRequestHandler {

    @Resource
    private QGroupUserOperationQuantityService qGroupUserOperationQuantityService;

    /**
     * 处理事件
     * @param data 信息内容
     * @param groupId 群号
     * @param userId 发送人QQ号
     */
    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {
        DateTime today = DateUtil.offsetHour(DateUtil.date(), 8);
        QueryWrapper<QGroupUserOperationQuantity> qGroupUserOperationQuantityQueryWrapper = new QueryWrapper<>();
        qGroupUserOperationQuantityQueryWrapper.eq("group_id", groupId);
        qGroupUserOperationQuantityQueryWrapper.eq("user_id", userId);
        qGroupUserOperationQuantityQueryWrapper.eq("statistics_date", DateUtil.format(today, "yyyy-MM-dd"));
        List<QGroupUserOperationQuantity> qGroupUserOperationQuantityList = qGroupUserOperationQuantityService.list(qGroupUserOperationQuantityQueryWrapper);
        if (CollUtil.isEmpty(qGroupUserOperationQuantityList)) {
            return;
        }
        qGroupUserOperationQuantityList = qGroupUserOperationQuantityList.stream().sorted(Comparator.comparing(QGroupUserOperationQuantity::getQuantity).reversed()).collect(Collectors.toList());
        Map<String, Object> templateParam = new HashMap<>();
        templateParam.put("requestList", qGroupUserOperationQuantityList);
        try {
            HtmlToImageUtils.htmlToImage("myRequestTemplate.html", templateParam, "src/main/resources/picture/myRequest.png", 1500);
            File file = new File("src/main/resources/picture/myRequest.png");
            String encode = Base64.encode(file);
            String msg = "[CQ:at,qq=" + userId + "]\n[CQ:image,file=base64://" + encode+ ",type=show,subType=0]";
            QQClientUtils.sendMsg(msg, userId, groupId, isGroup);
        } catch (IOException | ParserConfigurationException | SAXException | TemplateException e) {
            log.error("获取订单展示模板失败:{}", e.getMessage(), e);
//            QQClientUtils.sendGroupMsg("[CQ:at,qq=" + userId + "]\n获取订单展示模板失败，请联系制作人", userId, groupId, isGroup);
        }
    }

}
